<?php
	
	/* Includes */
	require_once("/../Data/Html.php");
	
	/*
	 	View for the Fileupload Module.
	 */
	 
	class FileUploadView extends Html
	{
		/* Member varaibles */
		private static $upload = "Upload";
		private static $file = "file";
		private static $fileToDelete = "";
		
		/* Generates a upload box */
		public function DoFileUploadBox()
		{
			// Generate and return the upload box
			return Html::Div(
			"<form method='post' action='' enctype='multipart/form-data'>
				<fieldset>
					<legend>File Upload</legend>
					<label>Filename:</label>
					<input type='file' name='" . self::$file . "' />
					<input type='submit' name='" . self::$upload . "' value='Submit' />
				</fieldset>
			</form>", "uploadDiv");
		}
		
		/* Generates a file list */
		public function DoFileList($files, $isLoggedIn)
		{
			// Content to be returned
			$content = "File List:";
			
			// Go through the files
			foreach ($files as $file)
			{
				// Skip the hidden "back files"
				if ($file == "." || $file == "..")
					continue;
				
				// Show the files in links
				$content .= "<div class='fileDiv'>";
				$content .= "<a href='Uploads/" . $file . "'>";
				$content .= $file;
				$content .= "<a>";
				
				// If the user is logged in, give him/her the option to delete a file
				if ($isLoggedIn)
				{
					$content .=
					"<form method='post' enctype='multipart/form-data'>
						<input type='submit' value='delete' name='" . $file . "' class='deleteButton' />
					 </form>";
				}
				
				$content .= "</div>";
			}
			
			return $content;
		}
		
		/* Gives the user feedback when uploading a file */
		public function DoUploadFeedback($didUpload)
		{
			return Html::Div($didUpload ? "Upload Successfull." : "Upload failed.");
		}
		
		/* Returns a value that indicates if the user tried to upload a file or not */
		public function TriedToUpload()
		{
			return isset($_POST[self::$upload]);
		}
		
		/* Returns a value that indicates if the user tried to delete a file or not */
		public function TriedToDelete($files)
		{
			// Go through the different delete buttons
			foreach ($files as $file)
			{
				if ($file == "." || $file == "..")
					continue;
				
				// Adjust the file name 
				$postFile = str_replace(".", "_", $file);
				$postFile = str_replace(" ", "_", $postFile);
				
				// If the button was pressed
				if (isset($_POST[$postFile]))
				{
					// Set the file to be deleted
					$_SESSION[self::$fileToDelete] = $file; 
					return true;
				}
			}
			
			return false;
		}
		
		/* Get the file name */
		public function GetFileName()
		{
			return self::$file; 
		}
		
		/* Get the name of the file to be deleted */
		public function GetFileToDelete()
		{
			return $_SESSION[self::$fileToDelete]; 
		}
	}

